package jltx.fragmentpageframework.widget;

import android.animation.ArgbEvaluator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.ImageView;

import jltx.fragmentpageframework.R;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/21.
 */

@SuppressLint("AppCompatCustomView")
public class ChangeColorImageView extends ImageView{

    private int selectMskColor;
    private int unSelectMskColor;
    private float offset = 0;  //0....1
    private ArgbEvaluator mArgbEvaluator = new ArgbEvaluator();

    public ChangeColorImageView(Context context) {
        super(context);
        initArray(context,null);
    }

    public ChangeColorImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initArray(context,attrs);
    }

    public ChangeColorImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initArray(context,attrs);
    }

    private void initArray(Context context, AttributeSet attrs){
        unSelectMskColor = ContextCompat.getColor(context, R.color.colorGrayWeChat);
        selectMskColor = ContextCompat.getColor(context, R.color.colorGreenWeChat);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ChangeColorView);
        int n = a.getIndexCount();

        for (int i=0; i < n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.ChangeColorView_selectMaskColor:
                    selectMskColor = a.getColor(attr,selectMskColor);
                    break;
                case R.styleable.ChangeColorView_unSelectMaskColor:
                    unSelectMskColor = a.getColor(attr,unSelectMskColor);
                    break;
            }
        }
        a.recycle();

        updateImageViewColorFilter();
    }

    public void setIconMaskColorRes(@ColorRes int unselectColor, @ColorRes int selectColor){
        unSelectMskColor = getResources().getColor(unselectColor);
        selectMskColor = getResources().getColor(selectColor);
        updateImageViewColorFilter();
    }

    public void setIconMaskColor(@ColorInt int unselectColor, @ColorInt int selectColor){
        unSelectMskColor = unselectColor;
        selectMskColor = selectColor;
        updateImageViewColorFilter();
    }


    /**
     * 改变ImageView的蒙层颜色
     * @param offset
     */
    public void changeImageViewDrawable(float offset){
        if(this.offset != offset){
            this.offset = offset;
            updateImageViewColorFilter();
        }
    }

    private void updateImageViewColorFilter(){
        int maskColor = (int) mArgbEvaluator.evaluate(offset,  unSelectMskColor,selectMskColor);
        setColorFilter(maskColor);
    }

}
